package com.soft.mainsite.module.web.cms.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.soft.mainsite.common.utils.CmsUtil;
import com.soft.mainsite.common.utils.JsonUtil;
import com.soft.mainsite.module.web.cms.service.TopicService;
import com.soft.mainsite.mybatis.model.TCmsTopic;
import com.soft.mainsite.mybatis.mapper.TCmsTopicMapper;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Description:
 *
 * @author Jin
 * @create 2017-07-19
 **/
@Service

public class TopicServiceImpl implements TopicService {

    @Autowired
    private TCmsTopicMapper topicMapper;


    @Override
    public String save(TCmsTopic pojo) {
        if (topicMapper.insertSelective(pojo) > 0) {
            return JsonUtil.toSuccess("操作成功！", "topic-tab", true);
        }
        return JsonUtil.toError("操作失败！");
    }


    @Override
    public String update(TCmsTopic pojo) {
        if (topicMapper.updateByPrimaryKeySelective(pojo) > 0) {
            return JsonUtil.toSuccess("操作成功！", "topic-tab", true);
        }
        return JsonUtil.toError("操作失败！");
    }


    @Override
    public String delete(Integer[] ids) {
        if (CmsUtil.isNullOrEmpty(ids)) {
            return JsonUtil.toError("操作失败！");
        }
        for (Integer id : ids) {
            topicMapper.deleteByPrimaryKey(id);
        }
        return JsonUtil.toSuccess("操作成功！", "topic-tab", false);
    }


    @Override
    public TCmsTopic findById(Integer id) {
        return topicMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<TCmsTopic> findList(TCmsTopic pojo) {
        return topicMapper.select(pojo);
    }

    @Override
    public List<TCmsTopic> findAll() {
        return topicMapper.selectAll();
    }

    @Override
    public PageInfo<TCmsTopic> page(Integer pageNumber, Integer pageSize, TCmsTopic pojo) {
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(this.findList(pojo));
    }

    @Override
    public PageInfo<TCmsTopic> page(Integer pageNumber, Integer pageSize) {
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(this.findAll());
    }


    @Override
    public Integer allCount() {
        return this.topicMapper.selectCount(new TCmsTopic());
    }


    @Override
    public List<TCmsTopic> findByRecommendList(Integer siteId, boolean isRecommend, Integer pageSize) {
        PageHelper.startPage(1, pageSize);
        TCmsTopic topic = new TCmsTopic();
        topic.setSiteId(siteId);
        topic.setIsRecommend(isRecommend);
        return this.topicMapper.select(topic);
    }

    /**
     * 查询专题列表
     *
     * @param
     * @return 专题列表
     * @author 胡佳明
     * @date 2019/5/9 17:43
     */
    @Override
    public PageInfo<TCmsTopic> pageList(Integer pageNumber, Integer pageSize, TCmsTopic pojo) {
        PageHelper.startPage(pageNumber, pageSize);
        List<TCmsTopic> topicList = topicMapper.topicList(pojo);
        return new PageInfo<>(topicList);
    }
}
